package com.youhong.teethcare.customviews;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.util.DisplayMetrics;

/**
 * Created by Administrator on 2018/4/11 0011.
 */

public class CountDownmageView extends AppCompatImageView {

    public CountDownmageView(Context context) {
        super(context);
    }

    public CountDownmageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CountDownmageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void setText(String text) {
        this.text = text;
    }

    String text = "0";
    RectF rectf = null;
    Paint paint = new Paint();
    Paint paint_ring = new Paint();


    float resolution_ratio;

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        rectf = new RectF(0, 0, w, h);
        DisplayMetrics displayMetrics = this.getContext().getResources().getDisplayMetrics();

        int screenWidth = displayMetrics.widthPixels;
        int screenHeight = displayMetrics.heightPixels;
        float scoreWidth = (float) screenWidth / 480;
        float scoreHeight = (float) screenHeight / 800;
        resolution_ratio = Math.min(scoreWidth, scoreHeight);


        paint.setTextSize(50f * resolution_ratio);
        paint.setAntiAlias(true);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setColor(Color.WHITE);

        paint_ring.setColor(Color.WHITE);
        paint_ring.setAntiAlias(true);
        paint_ring.setStrokeWidth(5f * resolution_ratio);
        paint_ring.setStyle(Paint.Style.STROKE);


        // act.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        // TextSize adaption for any Resolutions screen.
    }

    @Override
    protected void onDraw(Canvas canvas) {

        super.onDraw(canvas);
        canvas.drawColor(Color.TRANSPARENT);
        float textWidth = paint.measureText(text);
        canvas.drawText(text, rectf.centerX() - textWidth / 2, rectf.height() * 0.6f, paint);
        canvas.drawCircle(rectf.centerX(), rectf.centerY(), rectf.width() / 2 - 5f, paint_ring);
    }
}

